package com.ocom.aiotweb.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.aiotweb.feignclient.AiotClientService;
import com.ocom.aiotweb.service.UserPageService;
import com.ocom.aiotweb.vo.userpage.AddUserVo;
import com.ocom.aiotweb.vo.userpage.OnShowVo;
import com.ocom.common.constant.MapKey;
import com.ocom.common.entity.aiot.EntityUserInfo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.StringUtils;
import com.ocom.security.utils.SecurityUtils;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserPageServiceImpl implements UserPageService {

    @Autowired
    private AiotClientService aiotClientService;

    // 添加用户（UserPage/addUser）
    @Override
    public Result UserPageAddUser(AddUserVo addUserVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_account(),addUserVo.getAccount());
        map.put(mapKey.getKey_saasNo(),addUserVo.getSaasNo());
        map.put(mapKey.getKey_password(),addUserVo.getPassword());
        map.put(mapKey.getKey_userName(),addUserVo.getUserName());
        map.put(mapKey.getKey_userType(),addUserVo.getUserType());
        map.put(mapKey.getKey_comId(),addUserVo.getComId());
        map.put(mapKey.getKey_deptId(),addUserVo.getDeptId());

        map.put(mapKey.getKey_bnsId(),addUserVo.getBnsId());
        map.put(mapKey.getKey_status(),addUserVo.getStatus());
        if (StringUtils.isNotEmpty(addUserVo.getEndTime())){
            map.put(mapKey.getKey_endTime(),addUserVo.getEndTime());
        }

        map.put(mapKey.getKey_remark(),addUserVo.getRemark());

        return aiotClientService.AddUserInfo(map);
    }

    //修改密码（UserPage/modify）
    @Override
    public Result UserPageModify(String password) {
        System.out.println(SecurityUtils.getUsername());
        Result result = aiotClientService.GetUserInfo(SecurityUtils.getUsername());
        if(result.getCode()!=200){
            return ResultUtil.error(result.getCode());
        }
        EntityUserInfo entityUserInfo = JSON.parseObject(JSON.toJSONString(result.getData()), EntityUserInfo.class);

        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_userId(), entityUserInfo.getUserId());
        map.put(mapKey.getKey_saasNo(), entityUserInfo.getSaasNo());
        map.put(mapKey.getKey_password(), password);
        return aiotClientService.UpdateUserInfo(map);
    }

    // 用户列表（UserPage/onShow）
    @Override
    public Result UserPageOnShow(OnShowVo onShowVo) {
        Map<String, Object> map = new HashMap<>();
        MapKey mapKey = new MapKey();
        map.put(mapKey.getKey_account(), onShowVo.getAccount());
        map.put(mapKey.getKey_userType(), onShowVo.getUserType());
        map.put(mapKey.getKey_userName(), onShowVo.getUserName());
        map.put(mapKey.getKey_page(), onShowVo.getPage());
        map.put(mapKey.getKey_size(), onShowVo.getSize());
        return aiotClientService.GetUserList(map);
    }

    //用户角色列表（UserPage/roleInfo）
    @Override
    public Result UserPageRoleInfo(Long userId) {

        return aiotClientService.GetUserRoleByUserId(userId);
    }
}
